package com.yunzo.coc.common.utils;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.kit.PropKit;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushUtils {
	protected static final Logger LOG = LoggerFactory
			.getLogger(PushUtils.class);

	// 测试商会
	private static final String appKey = "8e6288e64c9260e82eb474a0";
	private static final String masterSecret = "322b2d6b661bb40a13e04c5e";

	public static final String TITLE = "Test from API example";
	public static final String ALERT = "Test from API Example - alert";
	public static final String MSG_CONTENT = "商会有一条新的商机发布了！";
	public static final String REGISTRATION_ID = "0900e8d85ef";
	public static final String TAG = "tag_api";

	private static Properties pinfo=PropKit.use("jpush.properties", "UTF-8").getProperties();
	public static void main(String[] args) {
		busSendPush("88888888888888888888888888888888",null,null,null);
	}

	public static void busSendPush(String groupid,String infoid,String contont,String alias) {
		JPushClient jpushClient = new JPushClient(pinfo.getProperty(groupid+"_masterSecret", "322b2d6b661bb40a13e04c5e"), pinfo.getProperty(groupid+"_AppKey", "8e6288e64c9260e82eb474a0"), 3);

		// 构建推送对象
		PushPayload payload = null;
		if(infoid!=null&&contont!=null&&alias!=null)
		{
			payload=buildPushforAliasObjectforinfo(groupid,infoid,contont,alias);
		}
		else if(infoid!=null&&contont!=null&&alias==null)
			payload=buildPushObjectforinfo(groupid,infoid,contont);
		else
			payload=buildPushObject_all_all_alert();

		try {
			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result -buildPushObject_all_all_alert: " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			LOG.error(
					"Error response from JPush server. Should review and fix it. ",
					e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
		}
	}
	/**
	 * 快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知。
	 * @return
	 */
	public static PushPayload buildPushObject_all_all_alert() {
		return PushPayload.alertAll(MSG_CONTENT);
	}
	/**
	 * 构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。
	 * @return
	 */
	public static PushPayload buildPushObject_all_alias_alert() {
		return PushPayload.newBuilder().setPlatform(Platform.all())
				.setAudience(Audience.alias("alias1"))
				.setNotification(Notification.alert(ALERT)).build();
	}
	/**
	 * 构建推送对象：平台是 Android，目标是 tag 为 "tag1" 的设备，内容是 Android 通知 ALERT，并且标题为 TITLE。
	 * @return
	 */
	public static PushPayload buildPushObject_android_tag_alertWithTitle() {
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.tag("tag1"))
				.setNotification(Notification.android(ALERT, TITLE, null))
				.build();
	}
	/**
	 * 构建推送对象：平台是 iOS，推送目标是 "tag1", "tag_all" 的交集，推送内容同时包括通知与消息 -
	 *  通知信息是 ALERT，角标数字为 1，通知声音为 "happy"，并且附加字段 from = "JPush"；消息内容是 MSG_CONTENT。
	 *  通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
	 * @return
	 */
	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.tag_and("tag1", "tag_all"))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(ALERT)
												.setBadge(1)
												.setSound("happy")
												.addExtra("from", "JPush")
												.build()).build())
				.setMessage(Message.content(MSG_CONTENT))
				.setOptions(
						Options.newBuilder().setApnsProduction(true).build())
				.build();
	}
	/**
	 * 构建推送对象：平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）且（"alias1" 与 "alias2" 的并集），
	 * 推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush。
	 * @return
	 */
	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(
						Audience.newBuilder()
								.addAudienceTarget(
										AudienceTarget.tag("tag1", "tag2"))
								.addAudienceTarget(
										AudienceTarget
												.alias("alias1", "alias2"))
								.build())
				.setMessage(
						Message.newBuilder().setMsgContent(MSG_CONTENT)
								.addExtra("from", "JPush").build()).build();
	}
	public static PushPayload buildPushObjectforinfo(String groupid,String infoid,String content) {
		return PushPayload.newBuilder().setPlatform(Platform.all())
                .setAudience(Audience.all()).
				setNotification(Notification
						.newBuilder()
						.addPlatformNotification(
								IosNotification.newBuilder()
										.setAlert(content)
										.setBadge(1)
										.setSound("happy")
										.addExtra("messge", infoid)
										.build())
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setAlert(content)
								.addExtra("message", infoid)
								.build()).build())
						.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}
	public static PushPayload buildPushforAliasObjectforinfo(String groupid,String infoid,String content,String alias) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios()).setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(alias)).build())
				.setNotification(Notification
						.newBuilder()
						.addPlatformNotification(
								IosNotification.newBuilder()
										.setAlert(content)
										.setBadge(1)
										.setSound("happy")
										.addExtra("messge", infoid)
										.build())
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setAlert(content)
								.addExtra("message", infoid)
								.build()).build())
						.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}
}
